#!/bin/sh

if [ $# -ne 2 ]; then
    echo "Usage: $0 /path/to/sqlcipher-for-android.zip /another/sqlcipher-for-android.zip"
    exit 1
fi

set -e
set -x

test -e "$1"
test -e "$2"

tmpdir=`mktemp -d /tmp/.compare-zips.XXXXXXXXXX`
zip1=$(basename $1)
zip2=$(basename $2)
sourcedir1=$(cd `dirname $1` && pwd)
sourcedir2=$(cd `dirname $2` && pwd)
zipname1=`echo $zip1 | sed 's,\.zip$,,'`
zipname2=`echo $zip2 | sed 's,\.zip$,,'`
dir1=$tmpdir/`echo $(dirname $1) | sed 's,[/ ],_,g'`-$zipname1
dir2=$tmpdir/`echo $(dirname $2) | sed 's,[/ ],_,g'`-$zipname2

mkdir -p $dir1
cd $dir1
unzip "$sourcedir1/$zip1"
# strip the full path to the zip for the comparison
unzip -l "$sourcedir1/$zip1" | sed 's,^\(Archive:\s\s*\)/.*/,\1,' > $dir1/unzip-l.txt
unzip -lv "$sourcedir1/$zip1" | sed 's,^\(Archive:\s\s*\)/.*/,\1,' > $dir1/unzip-lv.txt
mkdir -p $dir1/$zipname1/libs/sqlcipher
cd $dir1/$zipname1/libs/sqlcipher
unzip ../sqlcipher.jar
unzip -l ../sqlcipher.jar > unzip-l.txt
unzip -lv ../sqlcipher.jar > unzip-lv.txt
mkdir -p $dir1/$zipname1/libs/sqlcipher-javadoc
cd $dir1/$zipname1/libs/sqlcipher-javadoc
unzip ../sqlcipher-javadoc.jar
unzip -l ../sqlcipher-javadoc.jar > unzip-l.txt
unzip -lv ../sqlcipher-javadoc.jar > unzip-lv.txt

mkdir -p $dir2
cd $dir2
unzip "$sourcedir2/$zip2"
# strip the full path to the zip for the comparison
unzip -l "$sourcedir2/$zip2" | sed 's,^\(Archive:\s\s*\)/.*/,\1,' > $dir2/unzip-l.txt
unzip -lv "$sourcedir2/$zip2" | sed 's,^\(Archive:\s\s*\)/.*/,\1,' > $dir2/unzip-lv.txt
mkdir -p $dir2/$zipname2/libs/sqlcipher
cd $dir2/$zipname2/libs/sqlcipher
unzip ../sqlcipher.jar
unzip -l ../sqlcipher.jar > unzip-l.txt
unzip -lv ../sqlcipher.jar > unzip-lv.txt
mkdir -p $dir2/$zipname2/libs/sqlcipher-javadoc
cd $dir2/$zipname2/libs/sqlcipher-javadoc
unzip ../sqlcipher-javadoc.jar
unzip -l ../sqlcipher-javadoc.jar > unzip-l.txt
unzip -lv ../sqlcipher-javadoc.jar > unzip-lv.txt

if which meld > /dev/null; then
    meld $dir1 $dir2
elif which opendiff > /dev/null; then
    opendiff $dir1 $dir2
else
    echo "ERROR: meld or opendiff required for the comparison!"
fi

rm -rf $tmpdir/
